#!/bin/bash
# .........................................................................
# Title: Master_CGS.sh
#
# Main executable file for CGS section; submits all jobs via SLURM
# .........................................................................

U=${1}
nodes=1
mailtype="ALL"

# if user has shell profile setup
if [ -f ~/shell_profile.sh ]; then

    # run profile
    source ~/shell_profile.sh

    # set user variables
    U=${whoami}
    echo "User: "${U}

    # basic variables
    system_part="<SLURM_PARTITION>"

    # paths
    code_path="<CODE_PATH>"
    erroroutput_path="$project_path/erroroutput"

    # make error output folder if it doesn't exist
    mkdir -p "${erroroutput_path}"

    partition_CGS="$system_part"
    time_CGS="0-48:00:00"
    ntasks_CGS=28
    mem_CGS="256000"

else
    echo "Please set up ~/shell_profile.sh file. Exiting."
    exitcd
fi

# sanity-checking that project_path variables exist
if [[ -z "$project_path" ]]; then
    echo "Empty project_path variable: Exiting..."
    exit 1
fi

CGS_ID=`sbatch \
         --partition=${partition_CGS} --time=${time_CGS} \
         --nodes=${nodes} --ntasks=${ntasks_CGS} --job-name=CGS \
         --output="${erroroutput_path}/CGS-%A_%a.out" --error="${erroroutput_path}/CGS-%A_%a.err" \
         --mail-user=${mailuser} --mail-type=${mailtype} --requeue --mem=${mem_CGS} \
         "${code_path}/cgs/CGS_Controller.sh" ${U} CGS | awk '{print $NF}'`
echo "Submitted CGS Job: "${CGS_ID}
sleep 1

echo "All jobs submitted!"

